﻿using Appraisal_System.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Appraisal_System
{
    //自定义“委托”类型关键字
    public delegate void DelBindDgv();
    public partial class FrmUserManager : Form
    {
        //声明 “委托‘' 变量,实现保存即刷新首页窗口
        DelBindDgv delBindDgv;
        public FrmUserManager()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void FrmUserManager_Load(object sender, EventArgs e)
        {
            //身份 -- 下拉菜单
            BindCbx();
            //筛选 -- 显示表格
            BindDgv();
            delBindDgv = BindDgv;
        }

        private void BindDgv()
        {
            //筛选功能
            String userName = txtUserName.Text.Trim();
            int baseTypeId = (int)cbxBase.SelectedValue;
            bool isStop = chkIsStop.Checked;

            dgvUserAppraisal.AutoGenerateColumns = false;

            /*//查询表格所有数据
            dgvUserAppraisal.DataSource = UserAppraisalBases.GetListJoinAppraisal();*/

            //实现筛选功能
            List<UserAppraisalBases> userAppraisalBases = UserAppraisalBases.GetListJoinAppraisal();
            if (baseTypeId == 0)
            {
                //表格数据呈现
                dgvUserAppraisal.DataSource = userAppraisalBases.FindAll(m => m.UserName.Contains(userName) && m.IsDel == isStop);
            }
            else
            {
                dgvUserAppraisal.DataSource = userAppraisalBases.FindAll(m => m.UserName.Contains(userName) && m.BaseTypeId == baseTypeId && m.IsDel == isStop);
            }
        }

        private void BindCbx()
        {
            List<AppraisalBases> appraisalBases = new List<AppraisalBases>();
            appraisalBases = AppraisalBases.ListAll();

            //下拉菜单赋值

            /* 数据库表之间的联系，排除
             * cbxBase.Text = "--查询所有--";
              cbxBase.Items.Add("--查询所有--");
              //法一、循环给下拉列表赋值
              foreach (var appraisalBase in appraisalBases)
              {
                  cbxBase.Items.Add(appraisalBase.BaseType);
              }*/

            appraisalBases.Insert(0, new AppraisalBases
            {
                Id = 0,
                BaseType = "--查询所有--",
                AppraisalBase = 0,
                IsDel = false
            });
            //法二：单向绑定
            cbxBase.DataSource = appraisalBases;
            cbxBase.DisplayMember = "BaseType";
            cbxBase.ValueMember = "Id";
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindDgv();
        }

        //鼠标在子窗口的其他位置，右击事件
        private void dgvUserAppraisal_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                tsmAdd.Visible = true;
                tsmEdit.Visible = false;
                tsmStart.Visible = false;
                tsmStop.Visible = false;
            }
        }

        //鼠标在子窗口表格处右击事件
        private void dgvUserAppraisal_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if(e.RowIndex > -1)
                {
                    dgvUserAppraisal.Rows[e.RowIndex].Selected = true;

                    tsmAdd.Visible = true;
                    tsmEdit.Visible = true;

                    //显示启用或停用按钮
                    bool isDel = (bool)dgvUserAppraisal.SelectedRows[0].Cells["IsDel"].Value;
                    if (isDel)
                    {
                        tsmStart.Visible = true;
                    }
                    else
                    {
                        tsmStop.Visible = true;
                    }
                }
            }
        }

        private void tsmAdd_Click(object sender, EventArgs e)
        {
            FrmSetUser frmSetUser = new FrmSetUser(delBindDgv);
            //显示模式窗体,结束后，继续执行后续代码
            frmSetUser.ShowDialog();
          //  BindDgv();
        }

        /// <summary>
        /// 编辑按钮触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmEdit_Click(object sender, EventArgs e)
        {
            //当前选择中那行的 id
            int id = (int)dgvUserAppraisal.SelectedRows[0].Cells["Id"].Value;
            FrmSetUser frmSetUser = new  FrmSetUser(delBindDgv,id);
            frmSetUser.ShowDialog();
        }
    }
}
